/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
 * Description: 
 * Author: g00421808
 * Create: 11/17/2022
 * Notes: 
 */

#ifndef JSBIND_ARG_STORAGE_H
#define JSBIND_ARG_STORAGE_H

namespace Jsb {

class ArgStorage {
public:
    ArgStorage(uint32_t overloadId)
        : overloadId_(overloadId) {}

    virtual ~ArgStorage() = default;

    uint32_t GetOverloadGroupId() const { return(overloadId_); }

    virtual void* GetTemplate() = 0;

private:
    const uint32_t overloadId_;

};

template <typename C>
class TemplatedArgStorage : public ArgStorage {
public:
    TemplatedArgStorage(uint32_t overloadId)
        : ArgStorage(overloadId),
          data_(nullptr) {}

    ~TemplatedArgStorage() {
        if(data_)
            delete data_;
    }

    void* GetTemplate() override { return this; }

    template <typename... Args>
    void Init(Args&&... args) {
        data_ = new C(std::forward<Args>(args)...);
    }

    C* TaskClass() {
        C* temp = data_;
        data_ = nullptr;
        return temp;
    }

private:
    C* data_;
};
} // namespace Jsb
#endif //JSBIND_ARG_STORAGE_H
